package com.witmed.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.witmed.dao.DepartmentDao;
import com.witmed.entity.Department;
import com.witmed.entity.Hospital;
import com.witmed.util.DBUtils;

public class DepartmentDaoImpl implements DepartmentDao {

	private DBUtils db = new DBUtils();

	/**
	 * 新增科室
	 */
	@Override
	public int insertDepartment(Department department) {
		// TODO Auto-generated method stub
		return db.update("insert into department(department_id,department_name,hospital_id) values(null,?,?)",
				 department.getDepartmentName(), department.getHospital().getHospitalId());
	}

	/**
	 * 根据医院id查询科室
	 */
	@Override
	public List<Department> getfindByDoctorId(Hospital hospitalId) {
		// TODO Auto-generated method stub
		List<Department> list = new ArrayList<Department>();
		ResultSet rs = db.query("select department_id,department_name from department where hospital_id=?", hospitalId.getHospitalId());
		try {
			while (rs.next()) {
				Integer departmentId = rs.getInt("department_id");
				String departmentName = rs.getString("department_name");
				// 构建实体模型
				Department department = new Department(departmentId, departmentName, null);
				// 添加到集合中
				list.add(department);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据医院id删除科室
	 */
	@Override
	public int deleteByDepartmentId(int hospitalId) {
		// TODO Auto-generated method stub
		return db.update("delete from department where doctor_id=?", hospitalId);
	}

}
